var app = getApp();
var ptype;
var showtype = { 1: 4, 2: 2 };
var allacts;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    topNeedShow: true,
    scrolltop: 0,
    activeArr: { 4: { curpage: 0, isLast: false, actslist: [] },
     2: { curpage: 0, isLast: false, actslist:[] }},
    ptype: 1,
    actslist:[]
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    ptype = '1';
    if (options.ptype) {
      ptype = options.ptype;
    }
    
    this.setData({
      ptype: ptype
    });
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          scrollhgt: res.windowHeight
        });
      },
    });
    // this.loadList(ptype);
    this.setPageTitle(ptype)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  clearLoadMsg:function(){
    var activeArr= {
      4: { curpage: 0, isLast: false, actslist: [] },
      2: { curpage: 0, isLast: false, actslist: [] }
    };

    this.data.activeArr = activeArr;
    this.setData({
      actslist: []
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log('showtype[ptype]=' + showtype[this.data.ptype] + ' ptype=' + this.data.ptype);
    this.clearLoadMsg();
    this.loadGoodsToServer(showtype[this.data.ptype]);
  },
  loadGoodsToServer: function (loadtype) {
    console.log('loadtype=' + loadtype);

    var url = app.globalData.webUrl + 'Goods/goods_list';
    var curpage = this.data.activeArr[loadtype].curpage;
    var tdata = { 'type': loadtype, 'page': curpage, 'storeid': app.globalData.storeid };

    app.loadDataFromServer(url, this.loadCallBack, tdata);
    
  },
  loadCallBack: function (res) {

    wx.hideToast();
    if (res.status != 'ok') {
      wx.showToast({
        title: '网络错误~',
        image: '../imgs/tips.png'
      });
      return;
    }

    var actslist = res.result.arr;
    var gtype = res.result.type;
    for (var i = 0; i < actslist.length; i++) {
      //console.log('actslist[i].addtime=' + actslist[i].addtime);
      actslist[i].icon = app.globalData.ImgwebUrl + actslist[i].icon;
      actslist[i].showaddtime = app.formatDateTime(actslist[i].addtime);
    }

    this.data.activeArr[gtype].isLast = true;
    //查看是否翻到最后一页
    if (actslist.length == app.globalData.pageSize) {
      this.data.activeArr[gtype].curpage += 1;
      this.data.activeArr[gtype].isLast = false;
    }

    if (!this.data.activeArr[gtype].actslist){
      this.data.activeArr[gtype].actslist = [];
    }

    
    this.data.activeArr[gtype].actslist = this.data.activeArr[gtype].actslist.concat(actslist);
    
    this.setData({
      actslist: this.data.activeArr[gtype].actslist
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return app.getShareObj();
  },
  loadList: function (atype) {

  },
  goodsScroll: function (e) {
    var tmphgt = 46;
    var topNeedShow = true;
    if (e.detail.deltaY < 0 && e.detail.scrollTop > tmphgt) {
      topNeedShow = false;
    }
    this.setData({
      topNeedShow: topNeedShow
    });
  },
  loadMoreGoods: function () {
    
    var loadtype = showtype[this.data.ptype];
    console.log('loadtype=' + loadtype);
    console.log('im end' + this.data.activeArr[loadtype].isLast);
    if (this.data.activeArr[loadtype] && !this.data.activeArr[loadtype].isLast){
      console.log('this.data load');
      //showtype[this.data.ptype]
      this.loadGoodsToServer(loadtype);
    }
  },
  changeActType: function (e) {
    var tem_ptype = e.currentTarget.dataset.ptype;
    if (tem_ptype == this.data.ptype) {
      return;
    }
    var index = 4;
    if (tem_ptype == '2') {
      index = 2;
    }
    this.setPageTitle(tem_ptype);
    this.setData({
      actslist: [],
      ptype: tem_ptype,
      scrolltop: 0
    });

    if (!this.data.activeArr[index].actslist || this.data.activeArr[index].actslist.length == 0) {
      this.loadGoodsToServer(index);
      return;
    }
    else {
      this.setData({
        ptype: tem_ptype,
        actslist: this.data.activeArr[index].actslist,
        scrolltop: 0
      });
    }


  },
  showActDetail: function (e) {
    var goodsid = e.currentTarget.dataset.goodsid;
    if (this.data.ptype == '1') {
      wx.navigateTo({
        url: './newdetail/newdetail?goodsid=' + goodsid,
      });
      return;
    }
    wx.navigateTo({
      url: './detail/actdetail?goodsid=' + goodsid,
    });
  },
  setPageTitle(val){
    var titlestr = '最新资讯';
    if (val == '2') {
      titlestr = '最新优惠'
    }
    wx.setNavigationBarTitle({
      title: titlestr,
    });
  }
})